package zhou.cutfruit.models;

import java.util.ArrayList;

import zhou.cutfruit.thread.FruitThread;
import zhou.cutfruit.util.ConstantNumber;
import zhou.cutfruit.util.PreparedSoundSingle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

public class Movable {
	int start_x;//初始位置x
	int start_y; //初始位置y
	int x; //瞬时位置x
	int y;//瞬时位置y
	float start_vx = 2f;// 初始水平速度向量
	float start_vy = -10f;// 初始垂直速度向量
	float vx;// 水平速度向量
	float vy;// 垂直速度向量
	int r;// 水果半径

	float width;//水果宽度
	float height;//水果高度

	private int offset_whenCuted = 10;//被切掉之后两半西瓜飞开的加速度



	boolean isPlaySound = true; // 被划过之后 是否是第一次发出声音
	boolean isCuted = false;// 是否被划到
	boolean isStartFlying = false ;//是否刚开始飞起（用于播放飞起音效）
	int cuted_x;// 被切中时的X
	int cuted_y;// 被切中时的y
	FruitJams fruitJams; // 果酱

	private MotionEvent event;// 发生在该水果生命周期上的event

	Activity context;

	Paint paint;

	double start_time;// 获取该水果初始化的时间
	double time;// 运动时间
	ArrayList<Bitmap> watermelon_fulls = null;
	int bitmap_index = 0 ;
	
	
	
	Bitmap watermelon_left = null;
	Bitmap watermelon_right = null;

	FruitThread fruitThread = null;// 改变水果状态的线程
	Boolean isInScreen = false; // 是否出现在屏幕中(飞出)

	Bitmap[] fruitJambitmaps;// 果酱的图片


	
	
	
/**
 * 水果的初始化
 * @param x 横坐标
 * @param y 纵坐标
 * @param r 半径
 * @param vx 水平速度
 * @param vy 垂直速度
 * @param bitmaps_water 西瓜的动画帧图片集合
 * @param bitmap_half_left 西瓜的左半边 
 * @param bitmap_half_right 右半边
 * @param fruitJamsBitmaps 果酱的图片集合
 * @param activity
 */

	public Movable(int x, int y, int r, float vx, float vy, ArrayList<Bitmap> bitmaps_water,
			Bitmap bitmap_half_left, Bitmap bitmap_half_right,
			Bitmap[] fruitJamsBitmaps, Activity activity) {
		this.start_x = x;
		this.start_y = y;
		this.start_vx = vx;
		this.start_vy = vy;
		this.x = start_x;
		this.y = start_y;
		//如果初始x位置大于屏幕一般，则水平速度向左，反之向右
		if (start_x>ConstantNumber.SCREEN_WIDTH/2) {
			this.vx = -start_vx;
		}
		else {
			this.vx = start_vx;
		}
		
		this.vy = start_vy;
		this.r = r;
		this.watermelon_fulls = bitmaps_water;
		this.watermelon_left = bitmap_half_left;
		this.watermelon_right = bitmap_half_right;
		this.context = activity;
		start_time = System.nanoTime();
		this.fruitThread = new FruitThread(this, context);
		this.fruitThread.start();

		this.paint = new Paint();
		paint.setTextSize(30);
		paint.setColor(Color.BLUE);
		width = watermelon_fulls.get(0).getWidth();
		height = watermelon_fulls.get(0).getHeight();

		this.fruitJambitmaps = fruitJamsBitmaps;
		
		

	}

	/**
	 * 水果绘制自己
	 * 
	 * @param canvas
	 *            需要绘制在的画布
	 */
	public void drawSelf(Canvas canvas) {

		if (isCuted) {
			// canvas.drawText("被切过了", x, y, paint);
			this.setOffset_whenCuted(getOffset_whenCuted() + 6);

			canvas.drawBitmap(this.watermelon_right, x + getOffset_whenCuted(),
					y, paint);

			canvas.drawBitmap(this.watermelon_left, x - getOffset_whenCuted(),
					y, paint);
			//如果被切过了，还没有发声音，则播放被切声音，并设置播放声音为false
			if (isPlaySound) {
				PreparedSoundSingle.getInstance().playCuttingFruitSound();
				isPlaySound = false;
			}
		} else {

			//绘制当前帧
			canvas.drawBitmap(this.watermelon_fulls.get(bitmap_index), x, y, null);
			//如果刚刚飞出来，播放飞出音效，并设置刚刚飞出来为false
			if (isStartFlying) {
				PreparedSoundSingle.getInstance().playWaterMelonSound();
				setStartFlying(false);
			}
		}

	}

	/**
	 * 传入手指的x、y，判断是否切中水果
	 * 
	 * @param finger_x
	 *            手指x
	 * @param finger_y
	 *            手指y
	 * @return true:水果被手指切中    false:未切中
	 */
	public boolean judgeCutting(int finger_x, int finger_y) {
		if (!isCuted) {
			int distance_x = finger_x - this.getX();
			int distance_y = finger_y - this.y;
			if (distance_x >= 0 && distance_y >= 0
					&& distance_x < 2 * this.getR()
					&& distance_y < 2 * this.getR()) {

				this.setCuted(true);
				this.cuted_x = finger_x;
				this.cuted_y = finger_y;
				startJamShow();
				if (this.getVy() < 0) {
					this.setVy(0);
				}

			}

		}
		return this.isCuted;

	}

	//开始播放果酱动画，这个方法不该写在
	private void startJamShow() {
		FruitJams fruitJams = new FruitJams(fruitJambitmaps, this);
		fruitJams.startShow();
	}

	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	public void setCuted(boolean isCuted) {
		this.isCuted = isCuted;
	}

	public boolean isCuted() {

		return isCuted;
	}

	public void setPlaySound(boolean isPlaySound) {
		this.isPlaySound = isPlaySound;
	}

	public int getStart_x() {
		return start_x;
	}

	public void setStart_x(int start_x) {
		this.start_x = start_x;
	}

	public int getStart_y() {
		return start_y;
	}

	public void setStart_y(int start_y) {
		this.start_y = start_y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public float getStart_vx() {
		return start_vx;
	}

	public void setStart_vx(float start_vx) {
		this.start_vx = start_vx;
	}

	public float getStart_vy() {
		return start_vy;
	}

	public void setStart_vy(float start_vy) {
		this.start_vy = start_vy;
	}

	public float getVx() {
		return vx;
	}

	public void setVx(float vx) {
		this.vx = vx;
	}

	public float getVy() {
		return vy;
	}

	public synchronized void setVy(float vy) {

		this.vy = vy;
	}

	public int getR() {
		return r;
	}

	public void setR(int r) {
		this.r = r;
	}

	public double getStart_time() {
		return start_time;
	}

	public void setStart_time(double start_time) {
		this.start_time = start_time;
	}

	public double getTime() {
		return time;
	}

	public void setTime(double time) {
		this.time = time;
	}





	public FruitThread getFruitThread() {
		return fruitThread;
	}

	public void setFruitThread(FruitThread fruitThread) {
		this.fruitThread = fruitThread;
	}

	public Boolean getIsInScreen() {
		return isInScreen;
	}

	public void setIsInScreen(Boolean isInScreen) {
		this.isInScreen = isInScreen;
	}

	@Override
	public String toString() {
		return "Movable [start_x=" + start_x + ", start_y=" + start_y + ", x="
				+ x + ", y=" + y + ", start_vx=" + start_vx + ", start_vy="
				+ start_vy + ", vx=" + vx + ", vy=" + vy + ", r=" + r
				+ ", start_time=" + start_time + ", time=" + time + ", bitmap="
				+ watermelon_fulls + ", fruitThread=" + fruitThread
				+ ", isInScreen=" + isInScreen + "]";
	}

	public void setMotionEvent(MotionEvent event) {
		this.event = event;
	}

	public MotionEvent getEvent() {

		return this.event;

	}

	public synchronized void setOffset_whenCuted(int offset_whenCuted) {
		this.offset_whenCuted = offset_whenCuted;
	}

	public int getOffset_whenCuted() {
		return offset_whenCuted;
	}

	public int getCuted_x() {
		return cuted_x;
	}

	public void setCuted_x(int cuted_x) {
		this.cuted_x = cuted_x;
	}

	public boolean isStartFlying() {
		return isStartFlying;
	}

	public void setStartFlying(boolean isStartFlying) {
		this.isStartFlying = isStartFlying;
	}

	public int getCuted_y() {
		return cuted_y;
	}

	public void setCuted_y(int cuted_y) {
		this.cuted_y = cuted_y;
	}

	public FruitJams getFruitJams() {
		return fruitJams;
	}

	public void setFruitJams(FruitJams fruitJams) {
		this.fruitJams = fruitJams;
	}

	public int getBitmap_index() {
		return bitmap_index;
	}

	public void setBitmap_index(int bitmap_index) {
		this.bitmap_index = bitmap_index;
	}

	public int  getFruitBimtapSize(){
		return this.watermelon_fulls.size();
	}
}
